using System;
using System.Data.SQLite;

namespace 第十一次课堂作业
{
    public partial class Form1 : Form
    {
        SQLiteConnection m_dbConnection;
        int m_id = 1;

        public Form1()
        {
            InitializeComponent();
            KeyDown += new KeyEventHandler(textBox1_KeyDown); //增添回车键事件
            SQLiteConnection.CreateFile("MyDatabase.sqlite");  //创建Sqlite数据库
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");//建立与数据库的连接
            m_dbConnection.Open();
            createDB();
            string Sql = "select Chinese from words Where id = '1'";
            SQLiteCommand command = new SQLiteCommand(Sql, m_dbConnection);
            SQLiteDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                label3.Text = dr["Chinese"].ToString();
            }
            m_dbConnection.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e) //回车键触发事件
        {
            if (e.KeyCode == Keys.Enter)
            {
                confirm.Focus();
                confirm_Click(this, new EventArgs());
            }
        }

        public void createDB() //创建表及表的内容
        {
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
            m_dbConnection.Open();
            string createSql = "create table words (id int(2),English varchar(20), Chinese varchar(20))";
            SQLiteCommand command = new SQLiteCommand(createSql, m_dbConnection);
            command.ExecuteNonQuery();

            string sql = "insert into words (id,English, Chinese) values ('1','me', '我')";
            SQLiteCommand createCommand = new SQLiteCommand(sql, m_dbConnection);
            createCommand.ExecuteNonQuery();

            sql = "insert into words (id,English, Chinese) values ('2','myself', '我自己')";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into words (id,English, Chinese) values ('3','you', '你')";
            command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        private void confirm_Click(object sender, EventArgs e)
        {
            string your_anwser = textBox1.Text;
            string true_anwser = "";
            m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
            m_dbConnection.Open();
            string Sql = @"select English from words Where Chinese = '{0}'";
            Sql = string.Format(Sql, label3.Text);
            SQLiteCommand command = new SQLiteCommand(Sql, m_dbConnection);
            SQLiteDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                true_anwser = dr["English"].ToString();
            }
            if (your_anwser.Equals(true_anwser))
            {
                label4.Text = "正确!";
            }
            else
            {
                label4.Text = "错误!";
            }

        }

        private void next_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            label3.Text = "";
            string strCommond = "select count(*)  from words ";
            SQLiteCommand cmd = new SQLiteCommand(strCommond, m_dbConnection);
            int RowCount = Convert.ToInt32(cmd.ExecuteScalar());
            if (m_id < RowCount)
            {
                m_id++;

            }
            else if (m_id == RowCount)
            {
                m_id = 1;
            }
            Id.Text = m_id.ToString();
            string Sql = @"select Chinese from words Where id = '{0}'";
            Sql = string.Format(Sql, m_id);
            SQLiteCommand command = new SQLiteCommand(Sql, m_dbConnection);
            SQLiteDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                label3.Text = dr["Chinese"].ToString();
            }
        }
    }
}